﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default2 : System.Web.UI.Page, ICallbackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _grid.DataSource = _sampleData;
            _grid.DataBind(); 
           
        }
    }

      public DataTable _sampleData  
     {  
         get {  
             DataTable dt = (DataTable) Session["DataTable"];  
             if(dt == null)  
             {
                 dt = new DataTable();
                 dt.Columns.Add(new DataColumn("Contact Name", typeof(string)));
                 dt.Columns.Add(new DataColumn("Company Name", typeof(string)));
                 dt.Columns.Add(new DataColumn("City", typeof(string)));
                 dt.Columns.Add(new DataColumn("Country", typeof(string)));

                 dt.Rows.Add(new object[] { "Maria Anders", "Alfreds Futterkiste", "Berlin", "Germany" });
                 dt.Rows.Add(new object[] { "Ana Trujillo", "Emparedados y helados ", "México D.F.", "Mexico" });
                 dt.Rows.Add(new object[] { "Antonio Moreno", "Antonio Moreno Taquería", "México D.F.", "Mexico" });
                 Session["DataTable"] = dt;  
             }  
             return dt;  
            }  
         set  
         {  
             Session["DataTable"] = value;  
         }  
       }
  
     int _rowNumber = 0;
     protected void _grid_RowDataBound(object sender, GridViewRowEventArgs e)
     {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             for (int i = 0; i < e.Row.Cells.Count; i++)
             {
                 e.Row.Cells[i].Attributes.Add("ondblclick", "javascript:MakeCellEditable(this);");
                 e.Row.Cells[i].Attributes.Add("id", _rowNumber + "_" + i);
             }
             _rowNumber += 1;
         }  
     }

     public string result;
      private void updateTable(string _data)  
       {  
           string[] _NewData = _data.Split('~');  
           DataTable _tempTable = _sampleData;  
           for (int i = 0; i < _NewData.Length; i++)  
           {  
               string [] _changedTxt = _NewData[i].Split('|');  
               string[] _rowCol = _changedTxt[0].Split('_');  
               _tempTable.Rows[Convert.ToInt32(_rowCol[0])][Convert.ToInt32(_rowCol[1])] = _changedTxt[1];  
           }  
           _sampleData = _tempTable;  
           result = "SUCCESS";  
       }     

      #region ICallbackEventHandler Members

      public string GetCallbackResult()
      {
          return result; 
      }

      public void RaiseCallbackEvent(string eventArgument)
      {
          string[] args = eventArgument.Split('$');
          if (args[0] == "updateTable") updateTable(args[1]);
      }

      #endregion
}
